package com.sx.manage.service.supplier;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.sx.manage.service.bank.IMBaseConsumerBankMapService;
import com.sx.manage.service.bank.IMConsumerBankMapService;
import com.sx.model.constants.MSystemConstants;
import com.sx.model.constants.type.account.MAccountNumStatus;
import com.sx.model.manage.mybatis.vo.biz.center.MAccountVO;
import com.sx.model.manage.mybatis.vo.business.MBusinessVO;
import com.sx.model.manage.mybatis.vo.consumer.MConsumerBankMapVO;
import com.sx.model.manage.mybatis.vo.consumer.MVerifOpenAccountVO;
import com.sx.model.manage.mybatis.vo.result.ResultObjectVO;
import com.sx.model.manage.mybatis.vo.result.ResultVerifyVO;

/**
 * 供应商信息 service 实现类
 * 
 * @author lenovo
 *
 */
@Service("supplierService")
public class MSupplierService {

	private Logger _log = LoggerFactory.getLogger(this.getClass());

	@Resource(name = "supplierServiceImpl")
	private IMSupplierService supplierServiceImpl;

	@Resource(name = "consumerBankMapServiceImpl")
	private IMConsumerBankMapService consumerBankMapServiceImpl;

	@Resource(name = "mBaseConsumerBankMapServiceImpl")
	private IMBaseConsumerBankMapService mBaseConsumerBankMapServiceImpl;

	//@Resource(name = "accountService")
	///private IAccountServiceInterface accountService;

	public IMBaseConsumerBankMapService getmBaseConsumerBankMapServiceImpl() {
		return mBaseConsumerBankMapServiceImpl;
	}

	public void setmBaseConsumerBankMapServiceImpl(IMBaseConsumerBankMapService mBaseConsumerBankMapServiceImpl) {
		this.mBaseConsumerBankMapServiceImpl = mBaseConsumerBankMapServiceImpl;
	}

	public Logger get_log() {
		return _log;
	}

	public void set_log(Logger _log) {
		this._log = _log;
	}

	public IMSupplierService getSupplierServiceImpl() {
		return supplierServiceImpl;
	}

	public void setSupplierServiceImpl(IMSupplierService supplierServiceImpl) {
		this.supplierServiceImpl = supplierServiceImpl;
	}

	public IMConsumerBankMapService getConsumerBankMapServiceImpl() {
		return consumerBankMapServiceImpl;
	}

	public void setConsumerBankMapServiceImpl(IMConsumerBankMapService consumerBankMapServiceImpl) {
		this.consumerBankMapServiceImpl = consumerBankMapServiceImpl;
	}

//	public IAccountServiceInterface getAccountService() {
//		return accountService;
//	}
//
//	public void setAccountService(IAccountServiceInterface accountService) {
//		this.accountService = accountService;
//	}

	/**
	 * 开通子账户
	 */
	@SuppressWarnings({ "unused" })
	public ResultVerifyVO<MVerifOpenAccountVO> account(Long[] ids) {
		ResultVerifyVO<MVerifOpenAccountVO> vo = new ResultVerifyVO<>();
		if (null == ids) {
			return new ResultVerifyVO<>("m.parameter.is.null");
		}
		List<MAccountVO> list_ = new ArrayList<>();
		for (Long id : ids) {
			if (id == null) {
				continue;
			}
			ResultObjectVO<MBusinessVO> resultVO = supplierServiceImpl.getSupplierById(id);
			if (null == resultVO.getObjVo()) {
				return new ResultVerifyVO<>("m.parameter.is.null");
			}
			MBusinessVO businessVO = resultVO.getObjVo();
			MConsumerBankMapVO mbmvo = mBaseConsumerBankMapServiceImpl.query_consumer_bank_vo(id);
			if (null == mbmvo) {
				return new ResultVerifyVO<>("sj.m.null.num.error");
			}
			MAccountVO MAccountVO = saveaccount(businessVO, mbmvo);
			list_.add(MAccountVO);
		}
		if (null == list_) {
			return new ResultVerifyVO<>("sj.m.open.account.data.error");
		}
		List<MAccountVO> accountList = null;
		try {
			//未完成
			accountList = null;// accountService.openAccounts(list_);
		} catch (Exception e) {
			_log.error("开户异常error={}",e);
		}
		_log.info("公司开户==开户已经返回==");
		if (null == accountList) {
			return new ResultVerifyVO<>("sj.m.open.account.error");
		}
		List<MVerifOpenAccountVO> list = new ArrayList<>();
		for (MAccountVO info_1 : accountList) {
			// 判断是否开户成功
			if (null != info_1.getBankAccountNo()) {
				MConsumerBankMapVO mv = mBaseConsumerBankMapServiceImpl.query_consumer_bank_vo(Long.valueOf(String.valueOf(info_1.getUserId())));
				_log.info("公司开户==开户已经返回==开户名称" + info_1.getAccountName() + "开户账号:" + info_1.getAccountNo() + "更新开户状态");

				MConsumerBankMapVO bankMap = new MConsumerBankMapVO(mv.getId(), Long.valueOf(String.valueOf(info_1.getUserId())), info_1.getAccountNo(), MAccountNumStatus.OPENED, info_1.getRelaAcct(), MSystemConstants.CONSUMER_NATURE_BUSINESS, new Date(), info_1.getBankAccountNo());
				bankMap.setId(mv.getId());
				bankMap.setConsumer_id(Long.valueOf(String.valueOf(info_1.getUserId())));
				bankMap.setAccount_no(info_1.getAccountNo());
				bankMap.setAccount_no_status(MAccountNumStatus.OPENED);
				bankMap.setCard_num(info_1.getRelaAcct());
				bankMap.setAccount_type(MSystemConstants.CONSUMER_NATURE_BUSINESS);
				bankMap.setOpen_account_time(new Date());
				bankMap.setBank_account_no(info_1.getBankAccountNo());

				consumerBankMapServiceImpl.consumerBankMapUpdate(bankMap);
				// 成功 开户信息返回
				vo.setSuccess(true);
				list.add(new MVerifOpenAccountVO(info_1.getAccountName(), "开户成功"));
				_log.info("公司开户=开户信息(成功)返回 ===开户名称:" + info_1.getAccountName() + "开户信息:" + info_1.getErrorInfo());
			} else {
				// 失败 开户信息返回
				if (null == info_1.getErrorInfo()) {
					_log.info("公司开户=开户信息(失败 无结果)返回 ===开户名称:" + info_1.getAccountName() + "开户信息:银行未正确返回数据");
					vo.setSuccess(false);
					list.add(new MVerifOpenAccountVO(info_1.getAccountName(), "银行未正确返回数据"));
				} else {
					vo.setSuccess(false);
					_log.info("公司开户=开户信息(失败 有结果)返回 ===开户名称:" + info_1.getAccountName() + "开户信息:" + info_1.getErrorInfo());
					list.add(new MVerifOpenAccountVO(info_1.getAccountName(), info_1.getErrorInfo()));
				}
			}
			vo.setData(list);
		}
		return vo;
	}

	/**
	 * 封装 account 对象
	 * 
	 * @param company
	 * @param userBankMap
	 * @param paperType
	 * @return
	 */
	public MAccountVO saveaccount(MBusinessVO businessVO, MConsumerBankMapVO bankMapVO) {
		if (null == businessVO && null == bankMapVO) {
			return null;
		}
		MAccountVO account = new MAccountVO();
		account.setUserId(businessVO.getId());
		account.setBankCode(bankMapVO.getBank_code());// businessVO.getBank().getBank_info().getBank_code()
		account.setAccountName(bankMapVO.getBank_consumer_name());// businessVO.getBank().getBank_consumer_name()
		account.setMobile(businessVO.getContacts_phone());
		account.setPaperType(businessVO.getPapers_type());
		account.setPaperNo(businessVO.getPapers_num());
		account.setRelaAcct(businessVO.getBank().getCard_num());
		account.setRelaAcctName(businessVO.getBank().getBank_consumer_name());
		account.setRelaAcctBank(businessVO.getBank().getCls_bank_name());
		account.setRelaAcctBankAddr(bankMapVO.getBank_name());// businessVO.getBank().getCity_info().getUp_city_po().getCity_name()+businessVO.getBank().getCity_info().getCity_name()+businessVO.getBank().getBank_info().getBank_name()
		account.setRelaAcctBankCode(bankMapVO.getBank_code());// businessVO.getBank().getBank_info().getBank_code()
		account.setEmail(businessVO.getEmail());
		account.setLawName(businessVO.getBusiness_legal());
		account.setLinkMan(businessVO.getBusiness_contacts());
		account.setPhone(businessVO.getContacts_phone());
		account.setAddr(businessVO.getBusiness_address());
		account.setClsBankName(businessVO.getBank().getCls_bank_name());
		account.setClsBankCode(businessVO.getBank().getCls_bank_code());

		return account;
	}
}
